package com.snxun.browser_light.widget.gesturedetector;

import android.view.GestureDetector;
import android.view.MotionEvent;

import com.snxun.browser_light.widget.eventbus.EventType;
import com.snxun.browser_light.widget.eventbus.SlideEvent;
import com.snxun.browser_light.widget.eventbus.ZoomEvent;

import org.greenrobot.eventbus.EventBus;

/**
 * 手势监听器
 * Created by Yangjw on 2020/11/24.
 */
public class SimpleOnGestureListener extends GestureDetector.SimpleOnGestureListener {

    private int mViewpageX, mViewpageY, mViewpageWidth, mViewpageHeight;

    public void setViewPagePosition(int mViewpageWidth, int mViewpageHeight) {

        this.mViewpageWidth = mViewpageWidth * 7 / 10;
        this.mViewpageHeight = mViewpageHeight * 7 / 10;

        this.mViewpageX = (mViewpageWidth / 2) - (mViewpageWidth * 7 / 10 / 2);
        this.mViewpageY = (mViewpageHeight / 2) - (mViewpageHeight * 7 / 10 / 2);
    }


    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        JudgeSendEvent("onFling", e1, e2);
        return super.onFling(e1, e2, velocityX, velocityY);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        JudgeSendEvent("onDown", e, null);
        return super.onDown(e);
    }

    //判断是否发出事件
    private void JudgeSendEvent(String action, MotionEvent e1, MotionEvent e2) {
        switch (action) {
            case "onFling":
                if (e1.getX() < mViewpageX && e1.getY() > mViewpageY && e1.getY() < (mViewpageY + mViewpageHeight)) {
                    if (e2.getX() - e1.getX() > 20) {
                        EventBus.getDefault().post(new SlideEvent(MotionEvent.ACTION_MOVE, "right"));   //  发送消息
                    } else if (e1.getX() - e2.getX() > 20) {
                        EventBus.getDefault().post(new SlideEvent(MotionEvent.ACTION_MOVE, "left"));
                    }
                } else if (e1.getX() > (mViewpageX + mViewpageWidth) && e1.getY() > mViewpageY && e1.getY() < (mViewpageY + mViewpageHeight)) {
                    if (e2.getX() - e1.getX() > 20) {
                        EventBus.getDefault().post(new SlideEvent(MotionEvent.ACTION_MOVE, "right"));
                    } else if (e1.getX() - e2.getX() > 20) {
                        EventBus.getDefault().post(new SlideEvent(MotionEvent.ACTION_MOVE, "left"));
                    }
                }
                break;
            case "onDown":
                if (e1.getX() < mViewpageX && e1.getY() > mViewpageY && e1.getY() < (mViewpageY + mViewpageHeight)) {
                    EventBus.getDefault().post(new SlideEvent(MotionEvent.ACTION_DOWN, "left"));
                } else if (e1.getX() > (mViewpageX + mViewpageWidth) && e1.getY() > mViewpageY && e1.getY() < (mViewpageY + mViewpageHeight)) {
                    EventBus.getDefault().post(new SlideEvent(MotionEvent.ACTION_DOWN, "right"));
                } else if (mViewpageX < e1.getX() && e1.getX() < (mViewpageX + mViewpageWidth) && mViewpageY < e1.getY() && e1.getY() < (mViewpageY + mViewpageHeight)) {
                    EventBus.getDefault().post(new ZoomEvent(EventType.FRAGMENT_RESTORE));
                }

                break;
        }
    }


}
